package org.pcorp.battlespace.construction.modele;

import java.io.FileReader;
import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import org.pcorp.battlespace.modele.Capacite;
import org.pcorp.battlespace.modele.equipement.Armement;
import org.pcorp.battlespace.utils.CodeCnt;

@XmlRootElement(name="armements")
@XmlAccessorType(XmlAccessType.FIELD)
public class Armements {
	@XmlElementWrapper(name = "armements")
	@XmlElement(name = "arme")
	private List<Armement> armements; 
	
	
	
	public static List<Armement> getArmements(String file) {
		Armements arms = null;
		try {
			JAXBContext context = JAXBContext.newInstance(Armements.class);
			Unmarshaller um = context.createUnmarshaller();
			
			arms = ((Armements)um.unmarshal(new FileReader(file)));
		}
		
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return arms.getArmements();
	}

	public static void main(String[] args) throws Exception {
		String ARM_XML = "./arms.xml";
		Capacite capAM = new Capacite(1, "anti-missile", "A-M", 1);
		Capacite capAC = new Capacite(1, "anti-chasseur", "A-C", 1);
		List<Capacite> caps = new LinkedList<Capacite>();
		caps.add(capAM);
		caps.add(capAC);
		
	List<Armement> armes = new LinkedList<Armement>();
	armes.add(new Armement(1l, "WEAP-01", "TST01", "", 1000l, 100l, "Armement", CodeCnt.COD_TYP_NRJ, caps, 2l, CodeCnt.PORTE_C, 1l, 1l, 1l, 1l));
	armes.add(new Armement(2, "WEAP-02", "TST02", "", 2000, 200, "Armement", CodeCnt.COD_TYP_NRJ, caps, 2, CodeCnt.PORTE_C, 2, 2, 2, 2));
	armes.add(new Armement(3, "WEAP-03", "TST01", "", 1000, 100, "Armement", CodeCnt.COD_TYP_BAL, caps, 2, CodeCnt.PORTE_M, 3, 3, 3, 3));
	armes.add(new Armement(4, "WEAP-04", "TST01", "", 1000, 100, "Armement", CodeCnt.COD_TYP_BAL, caps, 2, CodeCnt.PORTE_M, 4, 4, 4, 4));
	
	List<Armement> listes = Armements.getArmements(ARM_XML);
	System.out.println(listes);
	
	JAXBContext jaxbContext = JAXBContext.newInstance(Armements.class);
	Marshaller marshaller = jaxbContext.createMarshaller();
	marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
	marshaller.marshal(	null, System.out);
	
	
	}


	public List<Armement> getArmements() {
		return armements;
	}



	public void setArmements(List<Armement> armements) {
		this.armements = armements;
	}
}
